#include "billsmodel.h"
#include <QDebug>

BillsModel::BillsModel(QObject *parent) :
    QAbstractTableModel(parent)
{
}


void BillsModel::load_data(const QDate &qd_target_filter, bool do_by_lease, bool do_by_nlease)
{
    QVariantList qvl_indices;
    SLEE_HDB_REC_LST qhl_data;
    if(qd_target_filter.isValid())
    {
        qvl_indices = mp_dbms->get_db(LAKDB_BILLS)->findDataEq(FLD_BILL_TARGETDATE, QVariant(qd_target_filter), &qhl_data);
    }
    else
    {
        qvl_indices = mp_dbms->get_db(LAKDB_BILLS)->findDataRange(-1, NULL,true,NULL,false,&qhl_data);
        qvl_indices.removeFirst();
    }
    mqvl_indices.clear();
    for(int i = 0; i< qvl_indices.size(); i++)
    {
        if(do_by_lease)
        {
            if(qhl_data.at(i).value(FLD_BILL_LINDEX).toULongLong() != 0ll)
            {
                mqvl_indices.append(qvl_indices.at(i));
                continue;
            }
        }
        if(do_by_nlease)
        {
            if(qhl_data.at(i).value(FLD_BILL_LINDEX).toULongLong() == 0ll)
            {
                mqvl_indices.append(qvl_indices.at(i));
                continue;
            }
        }
    }
    qDebug()<<"Bills model. bills loaded: "<<mqvl_indices.size();
}

int BillsModel::rowCount(const QModelIndex &/*parent*/) const
{
        return mqvl_indices.size();
}

int BillsModel::columnCount(const QModelIndex &/*parent*/) const
{
    //global id - 0
    //targed date 1
    //lease id - 2
    //ap_addr - 3
    //total space-4
    //ammount   5
    return 6;
}

QVariant BillsModel::data(const QModelIndex &index, int role) const
{
    static int last_row = -1;
    static SLEE_HDB_REC qh_ap_data;
    static SLEE_HDB_REC qh_bill_data;
    static QString qstr_lease;

    if((role != Qt::DisplayRole) &&
        (role!= Qt::ToolTipRole)) return QVariant();
    int r = index.row();
    int c = index.column();
    if(last_row !=r)
    {
        last_row = r;
        qh_bill_data = mp_dbms->get_data(LAKDB_BILLS, mqvl_indices.at(r));
        qh_ap_data = mp_dbms->get_data(LAKDB_APARTMENTS, qh_bill_data.value(FLD_BILL_AINDEX));
        SLEE_HDB_REC qh_l_data;
        QVariant qv_l_index = qh_bill_data.value(FLD_BILL_LINDEX);
        if(qv_l_index.toULongLong() != 0)
        {
            qstr_lease = RUSTR("№ ")+mp_dbms->get_data(LAKDB_LEASES, qv_l_index).value(FLD_LS_NUM).toString();
        }
        else
            qstr_lease = "---";
    }
    QString qstr;
    switch(c)
    {
    case 0: qstr = mqvl_indices.at(r).toString();
        break;
    case 1: qstr = qh_bill_data.value(FLD_BILL_TARGETDATE).toDate().toString("MM.yyyy");
        break;
    case 2: qstr = qstr_lease;
        break;
    case 3: qstr = mp_dbms->get_apartment_addr(qh_ap_data);
            break;
    case 4: qstr = QString::number(mp_dbms->get_apartment_lspace(qh_bill_data.value(FLD_BILL_AINDEX),qh_bill_data.value(FLD_BILL_SPSLST).toStringList()));
        break;
    case 5: qstr = QString::number(qh_bill_data.value(FLD_BILL_AMMOUNT).toDouble());
        break;
    }
    return QVariant(qstr);
}

QVariant BillsModel::headerData(int section, Qt::Orientation orientation, int role) const
{
    if((role != Qt::DisplayRole) &&
        (role!= Qt::ToolTipRole)) return QVariant();

    if(orientation == Qt::Vertical) return QVariant(section+1);
    if(orientation != Qt::Horizontal) return QVariant();

    QString qstr;
    switch (section)
    {
    case 0: qstr = RUSTR("Номер платежа"); break;
    case 1: qstr = RUSTR("Оплачиваемый период"); break;
    case 2: qstr = RUSTR("Номер договора"); break;
    case 3: qstr = RUSTR("Адрес"); break;
    case 4: qstr = RUSTR("Площадь (кв.м.)"); break;
    case 5: qstr = RUSTR("Сумма"); break;
    }
    return QVariant(qstr);
}

QVariant BillsModel::get_db_index(const QModelIndex &index)
{
    if(!index.isValid()) return QVariant();
    return mqvl_indices.at(index.row());
}

QModelIndex BillsModel::get_model_index(const QVariant &qv_index)
{
    int r = mqvl_indices.indexOf(qv_index);
    if(r <0) return QModelIndex();
    return createIndex(r,0);
}

